package game;

import game.SceneManager.SCENE;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.*;
import java.applet.*;
import java.util.*;

public class Game extends Applet implements Runnable
{

	private static final long serialVersionUID = 1L;
	public static final int screenWidth  = 800;
	public static final int screenHeigth = 600;
	public static Random random = new Random();
	public static int intPause = 1;
	float time;
	BufferedImage backbuffer;
	Graphics2D g2d;
	boolean escapePressed = false;
	Thread gameLoop;
	static Random rand = new Random();
	static Game instance;
	
	public static float timeTurno;
	
	Background pause;
	
	long lastTime;
	
	public static double loopTime;
	
	int fps = 60;
	long a, b;
	
	Image image;
	float x, y;
	int w, h;
	
	public void init()
	{
		
		
		this.backbuffer = new BufferedImage(screenWidth, screenHeigth, BufferedImage.TYPE_INT_RGB);
		this.g2d = backbuffer.createGraphics();
		addKeyListener(Keyboard.getInstance());
		addMouseListener(Mouse.getInstance());
		addMouseMotionListener(Mouse.getInstance());
		setFocusable(true);
		this.lastTime = System.nanoTime();
		//Audios.getInstance().loadSongs();
		SceneManager.setup();
		
		pause = new Background(4,new Point(0,0), new Point(800,600));
		pause.setImage(4);
	}
	public static Game getInstance() {
		return instance;
	}

	public void start()
	{
		this.gameLoop = new Thread(this);
		this.gameLoop.start();
	}

	public void stop()
	{
		this.gameLoop = null;
	}

	public void run()
	{
		Thread t = Thread.currentThread();

		while (t == this.gameLoop)
		{
			try 
			{
				Thread.sleep(15);
			}
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
			repaint();
		}	
	}
		
	public void update(Graphics g)
	{		
		paint(g);
	
		long currentTime = 0;
		currentTime = System.nanoTime();
		this.lastTime = currentTime;
		// FPS
		a = System.nanoTime();
		
		fps++;
		if (a - b >= 1000000000)
		{
			loopTime = (double)((1.0)/fps);
			//this.canvas.setTitle("The Legend of Silicon - fps: " + fps);
			fps = 1;
			b = a;
		}			
		
		if(intPause == 1)
			SceneManager.scene.update();
		
		time += Game.loopTime;
		
		timeTurno += Game.loopTime;
				
		if(SceneManager.currentScene != SCENE.SCN_OPENING && SceneManager.currentScene != SCENE.SCN_MENU_CREDIT
						 && SceneManager.currentScene != SCENE.SCN_MENU_INSTRUCTION
						 && SceneManager.currentScene != SCENE.SCN_MENU_CREDIT && SceneManager.currentScene != SCENE.SCN_INVESTIMENTO)
		{
			if(intPause == 1)
			{
				
				if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_ESCAPE) && time >1 && !escapePressed)
				{
					escapePressed = true;
					intPause = 0;
					time = 0;
				}
			}
			else
			{
				if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_ESCAPE) && time>1 && !escapePressed)
				{
					escapePressed = true;
					intPause = 1;
					time =0;
				}
			}
			
			if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_ESCAPE) == false)
				escapePressed = false;
		}
	}
	public void paint(Graphics g)
	{	
		SceneManager.scene.draw(g2d);	
		if(intPause == 0)
		{
			pause.draw(g2d);
			System.out.println("pause");
		}
		g.drawImage(backbuffer, 0, 0, this);
	}
}